<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Blog extends CI_Controller
{

    /**
     * 控制器初始化
     * Blog constructor.
     */
    public function __construct()
    {
        parent::__construct();

        $this->load->helper('url');
        $this->load->database();

        //加载模块
        $this->load->model('type_model');
        $this->load->model('blog_model');

        //调试模式
        //$this->output->enable_profiler(TRUE);
    }

    /**
     * （分类）显示博客表
     * @param int $tid
     */
    public function tid($tid = 0)
    {
        //分页
        $rows = 2;
        $page = empty($_GET['page']) ? 1 : $_GET['page'];
        $offset = ($page - 1) * $rows;
        $all_rows = $this->blog_model->all_rows($tid);

        //获取分类名
        $type = $this->type_model->get($tid);
        $name = empty($type) ? '博客' : $type['name'];

        //网页数据
        $data['tid'] = $tid;
        $data['page'] = $page;
        $data['all'] = ceil((double)$all_rows / (double)$rows);
        $data['typelist'] = $this->type_model->getlist();
        $data['bloglist'] = $this->blog_model->pagelist($tid, $rows, $offset);;
        $data['title'] = "Li Lijie - ".$name."列表";

        //载入
        $this->load->view('home/header', $data);
        $this->load->view('home/bloglist');
        $this->load->view('home/footer');
    }

    /**
     * 显示博客
     * @param string $id
     */
    public function id($id = '')
    {
        //网页数据
        $data['blog'] = $this->blog_model->get($id);
        $data['type'] = $this->type_model->get($data['blog']['tid']);
        $data['title'] = "Li Lijie - ".$data['blog']['title'];

        //载入
        $this->load->view('home/header', $data);
        $this->load->view('home/blog');
        $this->load->view('home/footer');
    }

}